package com.dyh.quickdemozwei.js;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: yhdong
 * @Date: 2021/9/16 18:00
 */
public class PimJsVisitors {

    @FunctionalInterface
    interface ValGetter{
        Object get(JSONObject jo);
    }


    public static  ValGetter nameCN = jo -> {
        JSONObject nName = jo.getJSONObject("name");
        return nName.get("zh_Hans_CN");
    };

    public static  ValGetter optionId = jo -> jo.get("option_id");

    public static  ValGetter value = jo -> jo.get("value");

    public static interface SelectIntTag{
        //
        //public static ValGetter[] getters = {optionId,nameCN,value};
        public static  ValGetter opVal = optionId;
        public static  ValGetter inVal = value;

    }
//    public static class SelectIntTag{
//        Object getSelectName(JSONObject jo){
//            JSONObject nName = jo.getJSONObject("name");
//            return nName.get("zh_Hans_CN");
//        }
//        Object getSelectId(JSONObject jo){
//           return   jo.get("option_id");
//        }
//
//        Object getInput(JSONObject jo){
//            return  jo.get("value");
//        }
//    }

    public static void main(String[] args) {
        String raw = "{\"type_id\":\"general\",\"spu_code\":\"spu_61249814d7f8f\",\"test_spu\":[{\"name\":{\"zh_Hans_CN\":\"选项1\",\"en_US\":null,\"de_DE\":null},\"option_id\":258,\"value\":32},{\"name\":{\"zh_Hans_CN\":\"选项2\",\"en_US\":null,\"de_DE\":null},\"option_id\":259,\"value\":43}],\"product_api_log\":[]}";

        JSONObject jo = JSON.parseObject(raw);

        //
        String fromTarget = "test_spu";
        String toTarget = "material_prop_149422948";
        Class type = SelectIntTag.class;

        JSONArray jsonArray = jo.getJSONArray("test_spu");

        List valsToSet = new ArrayList();
        for (Object o : jsonArray) {
            JSONObject joo = (JSONObject) o;
            Object inVal = SelectIntTag.inVal.get(joo);
            Object opVal = SelectIntTag.opVal.get(joo);
            JSONObject smObjNew = new JSONObject();
            smObjNew.put("material_prop_name",opVal);
            smObjNew.put("material_prop_content",inVal);

            valsToSet.add(smObjNew);
        }

        System.out.println();
    }
}
